<?php
class attachment_db extends Model
{
	public $pk = 'aid';
	public $table = 'ptx_attachment';

	function getattachtableid($itemid) {
		$itemid = (string)$itemid;
		return intval($itemid{strlen($itemid)-1});
	}
	
	function getUnused($aid){
		$this->inittable('unused');
		return $this->findBy('aid', $aid);
	}
	
	function getReal($aid,$itemid){
		$this->inittable($itemid);
		return $this->findBy('aid', $aid);
	}

	function updateReal($aid,$itemid,$data){
		$this->inittable($itemid);
		return $this->update(array('aid'=>$aid),$data);
	}
	
	function getIndex($aid){
		$this->inittable(false);
		return $this->findBy('aid', $aid);
	}
	
	function addToAttachIndexTable($data){
		$this->inittable(false);
		$insertData['table_id']=$data['table_id'];
		if($data['item_id']){ 
			$insertData['item_id']=$data['item_id'];
			$insertData['table_id']=$this->getattachtableid($data['item_id']);
		}
		$insertData['user_id']=$data['user_id'];
		if($data['downloads']) $insertData['downloads']=$data['downloads'];
		return $this->create($insertData);
	}
	
	function addToUnusedTable($data){
		$this->inittable('unused');
		$insertData['aid']=$data['aid'];
		$insertData['create_time']=TIMESTAMP;
		$insertData['user_id']=$data['user_id'];
		$insertData['filename']=$data['filename'];
		$insertData['fileext']=$data['fileext'];
		$insertData['filesize']=$data['filesize'];
		$insertData['attachment']=$data['attachment'];
		if($data['remote']) $insertData['remote']=$data['remote'];
		if($data['description']) $insertData['description']=$data['description'];
		if($data['readperm']) $insertData['readperm']=$data['readperm'];
		$insertData['isimage']=$data['isimage'];
		if($data['width']) $insertData['width']=$data['width'];
		if($data['height']) $insertData['height']=$data['height'];
		if($data['thumb']) $insertData['thumb']=$data['thumb'];
		if($data['downloads']) $insertData['downloads']=$data['downloads'];
		return $this->create($insertData);
	}
	
	function inittable($itemid) {
		$this->tbl_name = $this->dbpre . $this->table;
		if($itemid){
			if(is_numeric($itemid)){
				$this->tbl_name .= '_'.$this->getattachtableid($itemid);
			}else{
				$this->tbl_name .= '_'.$itemid;
			}
		}
	}
	
	public function findAttachByItem($itemid){
		$this->inittable($itemid);
		return $this->findAll(array('item_id'=>$itemid),' aid ASC ');
	}
	
	public function findSubImageCoverByItem($itemid,$aid){
		$this->inittable($itemid);
		return $this->findAll(' item_id='.$itemid.' AND isimage>0 AND aid<>'.$aid.' ',null,null,2);
	}
	
	public function addAttachByItem($itemid,$data){
		$this->inittable($itemid);
		$insertData['aid']=$data['aid'];
		$insertData['item_id'] = $itemid;
		$insertData['create_time']=TIMESTAMP;
		$insertData['user_id']=$data['user_id'];
		$insertData['filename']=$data['filename'];
		$insertData['fileext']=$data['fileext'];
		$insertData['filesize']=$data['filesize'];
		$insertData['attachment']=$data['attachment'];
		if($data['remote']) $insertData['remote']=$data['remote'];
		if($data['source']) $insertData['source']=$data['source'];
		if($data['description']) $insertData['description']=$data['description'];
		if($data['readperm']) $insertData['readperm']=$data['readperm'];
		$insertData['isimage']=$data['isimage'];
		if($data['width']) $insertData['width']=$data['width'];
		if($data['height']) $insertData['height']=$data['height'];
		if($data['thumb']) $insertData['thumb']=$data['thumb'];
		if($data['downloads']) $insertData['downloads']=$data['downloads'];
		return $this->create($insertData);
	}
	
	public function findUnusedByUser($uid){
		$this->inittable('unused');
		return $this->findAll(array('user_id'=>$uid));
	}
	
	public function updateIndex($aid,$itemid){
		$this->inittable(false);
		return $this->update(array('aid'=>$aid), array('item_id'=>$itemid,'table_id'=>$this->getattachtableid($itemid)));
	}

	public function updateDownloads($aid){
		if($index = $this->getIndex($aid)){
			$this->inittable(false);
			$this->incrField(array('aid'=>$aid), 'downloads');

			$this->inittable($index['item_id']);
			$this->incrField(array('aid'=>$aid), 'downloads');
		}
	}
	
	public function deleteUnused($aid){
		$this->deleteUnusedIndex($aid);
		$this->inittable('unused');
		return $this->deleteByPk($aid);
	}

	public function deleteReal($aid,$itemid){
		$this->inittable($itemid);
		if($this->deleteByPk($aid)){
			return $this->deleteIndex($aid, $itemid);
		}
		return false;
	}
	
	public function getAttachById($aid){
		$index = $this->getIndex($aid);
		$attach = $this->getReal($aid, $index['item_id']);
		$attach['downloads'] = $index['downloads'];
		return $attach;
	}

	private function deleteIndex($aid,$itemid){
		if($this->getReal($aid, $itemid)){
			return true;
		}
		$this->inittable(false);
		return $this->deleteByPk($aid);
	}
	
	private function deleteUnusedIndex($aid){
		if($index = $this->getIndex($aid)){
			if($index['table_id']>9){
				$this->inittable(false);
				return $this->deleteByPk($aid);
			}
		}
		return true;
	}

}
